package org.jeecg.modules.demo3.fly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.demo3.fly.entity.AxhkYwAgrplane;
import org.jeecg.modules.demo3.fly.mapper.AxhkYwAgrplaneMapper;
import org.jeecg.modules.demo3.fly.service.IAxhkYwAgrplaneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 无人机管理表
 * @Author: jeecg-boot
 * @Date:   2021-01-28
 * @Version: V1.0
 */
@Service
public class AxhkYwAgrplaneServiceImpl extends ServiceImpl<AxhkYwAgrplaneMapper, AxhkYwAgrplane> implements IAxhkYwAgrplaneService {

    @Autowired
    private AxhkYwAgrplaneMapper axhkYwAgrplaneMapper;

    //通过无人机序列号查询无人机是否存在-wjn
    @Override
    public boolean getByserialNumber(@Param(value = "workId") String workId, @Param(value = "serialNumber") Integer serialNumber) {
        String agrplaneId = axhkYwAgrplaneMapper.getByserialNumber(serialNumber);
        if(null != agrplaneId && null != workId){
            int i = axhkYwAgrplaneMapper.addAgrplane(workId,agrplaneId);
        }else{
            return false;
        }
        return true;
    }

    //通过作业Id查询，作业下的农机--wjn
    @Override
    public List<Map<String,Object>> getByWorkId(String id) {
        List<Map<String,Object>> axhkYwAgrplaneList = axhkYwAgrplaneMapper.getByWorkId(id);
        return axhkYwAgrplaneList;
    }

    //根据ID删除-wjn
    @Override
    public int getPlaneId(Integer id) {
        int n = -1;
        if(null != id){
            int i = axhkYwAgrplaneMapper.getByPlaneId(id);
            return i;
        }else {
         return n;
        }
    }

    //根据APP传输的 agrplane_id,user_id 修改无人机管理 APP用户跟用户组
    @Override
    public int byUserAgrplane(String userId, String agrplaneId) {
        String userName = axhkYwAgrplaneMapper.getUserName(userId);
        Map<String, String> depart = axhkYwAgrplaneMapper.getDepartName(userId);
        //查询 该无人机管理下 否存在用户
        String name = axhkYwAgrplaneMapper.getByUserName(agrplaneId);
        int i = 0;
        if(name.equals("无") || name.equals("")){
            i = axhkYwAgrplaneMapper.getUpdateAgrplane(agrplaneId,userName,depart.get("depart_name"),userId,depart.get("org_code"));
        }else{
            i = 0;
        }
        return i;
    }

    //根据无人机激活状态改变 激活时间
    @Override
    public int byUpdateTime(String num, Integer number) {
        //判断传过来的 状态是 激活 还是未激活状态
        if (num.equals("1")) {
            // 1 激活状态  2 未激活状态
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String times = df.format(new Date());// new Date()为获取当前系统时间
            int i = axhkYwAgrplaneMapper.getUpdateTime(times, number);
            return i;
        }
        if (num.equals("2")) {
            //否则为未激活状态
            //如果是变成未激活状态，把激活时间设为空值，等待下次激活
            int i = axhkYwAgrplaneMapper.getUpdateTime(null, number);
            return i;
        }
        return 0;
    }

    /*
    * 列举所有的设备
    * */
    @Override
    public List<AxhkYwAgrplane> getAllByAxhkYwAgrplane() {
        List<AxhkYwAgrplane> axhkYwAgrplaneListanes = axhkYwAgrplaneMapper.getAxhkYwAgrplanes();
        return axhkYwAgrplaneListanes;
    }

    /*
     * 根据序列号查设备
     * int serialNumber 序列号
     * */
    @Override
    public AxhkYwAgrplane getAxhkYwAgrplane(int serialNumber) {
        AxhkYwAgrplane axhkYwAgrplane = axhkYwAgrplaneMapper.getAllAxhkYwAgrplane(serialNumber);
        if(null != axhkYwAgrplane){
            return axhkYwAgrplane;
        }else{
            return null;
        }
    }

}
